package logic

import (
	"context"
	"doctor-medicine-system/model/cache"
	"doctor-medicine-system/model/model"
	"doctor-medicine-system/zhoukao3rpc/internal/svc"
	"doctor-medicine-system/zhoukao3rpc/zhoukao3rpc"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *zhoukao3rpc.LoginRequest) (*zhoukao3rpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	var user model.User

	result, _ := cache.Rdb.Get(l.ctx, "sendSms").Result()
	if result == "" {
		return nil, errors.New("验证码错误")
	}

	if user.ID != 0 {
		return nil, errors.New("用户不存在")
	}

	if err := user.UserLogin(model.DB, in.Phone); err != nil {
		return nil, err
	}

	return &zhoukao3rpc.LoginResponse{
		UserId: int64(user.ID),
	}, nil
}
